package cn.tianyigps.tianyigpsapiv2.utils;

import java.text.Collator;
import java.util.*;

/**
 * Created with IntelliJ IDEA
 * Created By yangxl
 * Date: 2019/1/15
 * Time: 14:35
 * Description:
 */
public class EncryptUtils {

    /**
     * desc: 根据参数来获取sign用以校验数据
     * @param params:传递待加密参数信息
     * @return
     **/
    public static String getSign(HashMap<String, String> params){
        if (params.size()==0){
            return "";
        }
        ArrayList<Map.Entry<String,String>> paramsListBefore= new ArrayList<>(params.entrySet());
//		System.out.println("-----> 字典序排序前的顺序");
//		for (int i = 0; i < paramsListBefore.size(); i++) {
//			System.out.println(paramsListBefore.get(i));
//		}
        ArrayList<Map.Entry<String,String>> paramsListAfter = sort(paramsListBefore);
//		System.out.println("-----> 字典序排序后的顺序");
//			for (int i = 0; i < paramsListAfter.size(); i++) {
//			System.out.println(paramsListAfter.get(i));
//		}
        StringBuffer sb = getParamsString(params);
        return DESUtils2.Md5(sb.toString());
    }

    private static StringBuffer getParamsString(HashMap<String, String> params) {
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            sb.append(entry.getKey()+"="+entry.getValue()+"&");
        }
        sb.substring(0, sb.length()-1);
        return sb;
    }

    /**
     * desc: url encode
     * @param
     * @return
     **/
    public static String getURLEncoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLEncoder.encode(str, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * desc: url decode
     * @param
     * @return
     **/
    public static String URLDecoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * desc: url decode
     * @param
     * @return
     **/
    public static String getURLDecoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * desc: 字典序排序
     * @param
     * @return
     **/
    private static ArrayList<Map.Entry<String,String>> sort(ArrayList<Map.Entry<String,String>> list){
        Collections.sort(list, new Comparator<Map.Entry<String,String>>() {
            @Override
            public int compare(Map.Entry<String,String> o1, Map.Entry<String,String> o2) {
                Comparator<Object> com = Collator.getInstance(Locale.CHINA);
                return com.compare(o1.getKey(), o2.getKey());
            }
        });
        return list;
    }

    /**
     * desc: 根据不同的厂商（接收端）的特征来生成相应的token，暂时以厂商名的MD5值作为token值
     * @param
     * @return
     **/
    public static String getToken(String name){
        return DESUtils2.Md5(name);
    }

    /**
     * desc: 获取时间戳
     * @param
     * @return
     **/
    public static String getTimeStamp(){
        return String.valueOf(System.currentTimeMillis());
    }

    /**
     * desc: 加密
     * @param data: 密文
     * @param key : 秘钥
     * @return 明文
     **/
    public static String Encrypt(String data, String key) throws Exception {
        return DESUtils2.encrypt(data, key);
    }

    /**
     * desc:解密
     * @param data : 明文
     * @param key : 秘钥
     * @return 密文
     **/
    public static String Decrypt(String data, String key) throws Exception {
        return DESUtils2.decrypt(data, key);
    }
}
